#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Kang.Cunhua
# @Date:   2015-11-15 11:58:45
# @Last Modified by:   Kang.Cunhua
# @Last Modified time: 2015-11-15 13:29:22

from twisted.web import server, resource
from twisted.internet import reactor, endpoints

# 下面这个class是整个程序的核心


class Counter(resource.Resource):
    """docstring for Counter"""

    isLeaf = True
    numberRequest = 0

    # 定义GET请求的处理函数
    def render_GET(self, request):
        print 'self.numberRequest:b: ', self.numberRequest
        self.numberRequest += 1  # 计数器加1
        # 设定http header
        request.setHeader('content-typy', 'text/plain')
        # 返回页面内容        #
        return "I am request #" + str(self.numberRequest) + '\n'
# 监听tcp端口8080
endpoints.serverFromString(reactor, 'tcp:8080').listen(server.Site(Counter()))
# 开启server
reactor.run()
